﻿using llm_net.Models;
using llm_net.Services.Abstractions;
using Microsoft.AspNetCore.Mvc;

namespace llm_net.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class RecommendationController: ControllerBase
    {
        private readonly IRecommendationService _recommendationService;

        public RecommendationController(IRecommendationService recommendationService)
        {
            _recommendationService = recommendationService;
        }
      /// <summary>
      /// 获取推荐
      /// </summary>
      /// <param name="userPreferences"></param>
      /// <param name="topK"></param>
      /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult<List<Product>>> GetRecommendations(
            [FromBody] List<TagWithWeightDto> userPreferences,
            [FromQuery] int topK = 5)
        {
            if (userPreferences == null || !userPreferences.Any())
            {
                return BadRequest("用户偏好不能为空");
            }

            try
            {
                var recommendations = await _recommendationService.GetRecommendationsAsync(userPreferences, topK);
                //将recommendations下的Vector赋为空值
                foreach (var item in recommendations)
                {
                    item.Vector = null;
                }
                return Ok(new
                {
                    Success = true,
                    Data = recommendations,
                    Message = "标签生成成功"
                });
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"获取推荐失败: {ex.Message}");
            }
        }
    }
}
